package com.example.demo.controller.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.config.tool.MyJson;
import com.example.demo.model.EduCourseKpoint;
import com.example.demo.service.EduCourseKpointService;
import com.example.demo.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    EduCourseKpointService eduCourseKpointService;
    @Autowired
    EduCourseService eduCourseService;

    @RequestMapping("all")
    public JSONObject all(
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
            @RequestParam(value = "size",required = false, defaultValue = "20") Integer size,
            @RequestParam(value = "sort_direction", required = false, defaultValue = "desc") String direction,
            @RequestParam(value = "sort_column", required = false, defaultValue = "") String column){
        if(column.equals("")){
            return MyJson.toJsonObject(eduCourseService.findAll(page, size, direction));
        }else {
            return MyJson.toJsonObject(eduCourseService.findAll(page, size, direction, column));
        }

    }

    @RequestMapping("kpoints")
    public JSONObject kpoints(
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
            @RequestParam(value = "size",required = false, defaultValue = "20") Integer size,
            @RequestParam(value = "sort_direction", required = false, defaultValue = "desc") String direction,
            @RequestParam(value = "sort_column", required = false, defaultValue = "") String column){
        if(column.equals("")){
            return MyJson.toJsonObject(eduCourseKpointService.findAll(page, size, direction));
        }else {
            return MyJson.toJsonObject(eduCourseKpointService.findAll(page, size, direction, column));
        }

    }


    @RequestMapping("kpoints-of")
    public JSONObject kpointsOf(
            @RequestParam(value = "course_id", required = true) Integer courseId,
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
            @RequestParam(value = "size",required = false, defaultValue = "20") Integer size,
            @RequestParam(value = "sort_direction", required = false, defaultValue = "desc") String direction,
            @RequestParam(value = "sort_column", required = false, defaultValue = "") String column){

        EduCourseKpoint kp = new EduCourseKpoint();
        kp.setCourseId(courseId);
        if(column.equals("")){
            return MyJson.toJsonObject(eduCourseKpointService.findAll(Example.of(kp), page, size, direction));
        }else {
            return MyJson.toJsonObject(eduCourseKpointService.findAll(Example.of(kp), page, size, direction, column));
        }
    }
}
